#include <asio.hpp>
#include <iostream>

using asio::ip::tcp;

int main(int argc, char *argv[]) 
{
    try {
        if (argc != 3) {
            std::cerr << "Usage: client <host> <port>" << std::endl;
            return 1;
        }
        asio::io_context io;
        tcp::resolver resolver(io);
        tcp::socket socket(io);

        tcp::resolver::results_type endpoints = resolver.resolve(argv[1], argv[2]);
        asio::connect(socket, endpoints);

        for (;;) {
            std::array<char, 128> buf;
            std::error_code ec;

            size_t n = socket.read_some(asio::buffer(buf), ec);

            if (ec == asio::error::eof) {
                break;
            } else if (ec) {
                throw std::system_error(ec);
            }

            std::cout.write(buf.data(), n);
        }

    } catch (std::exception &ex) {
        std::cerr << ex.what() << std::endl;
    }
}
